package mobweb.booktrack;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainMenu extends Activity implements AsyncTaskCompleteListener<Book> {
    
    Activity acT = this;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main_menu);

	Button button1 = (Button) findViewById(R.id.booklistbutton);
	Button button2 = (Button) findViewById(R.id.scanisbnbutton);
	Button button3 = (Button) findViewById(R.id.searchbookbutton);
	Button button4 = (Button) findViewById(R.id.addbookbutton);

	button1.setOnClickListener(new OnClickListener() {
	    //book list button
	    @Override
	    public void onClick(View v) {
		Intent i = new Intent(getApplicationContext(), BookListAct.class);
	    	startActivity(i);
	    }

	});

	button2.setOnClickListener(new OnClickListener() {
	    //scan isbn button
	    @Override
	    public void onClick(View v) {
		ZXingIntentIntegrator integrator = new ZXingIntentIntegrator(acT);
		integrator.initiateScan();
	    }

	});

	button3.setOnClickListener(new OnClickListener() {
	    //book list button
	    @Override
	    public void onClick(View v) {
		Intent search = new Intent(getApplicationContext(), SearchActivity.class);
	    	startActivity(search);
	    }

	});

	button4.setOnClickListener(new OnClickListener() {
	    //add book button
	    @Override
	    public void onClick(View v) {
	    	Intent addBook = new Intent(getApplicationContext(), AddBookActivity.class);
	    	startActivity(addBook);
	    	
//		Toast.makeText(getBaseContext(), "Clickety click 4",
//			Toast.LENGTH_LONG).show();
	    }

	});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.activity_main_menu, menu);
	return true;
    }
    
    /* Sorry Markuss I think this way is better
     * @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	   if (requestCode == 0) {
	      if (resultCode == RESULT_OK) {
	         String contents = intent.getStringExtra("SCAN_RESULT");
	         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	         
	         // Handle successful scan, show BookActivity
	         Intent i = new Intent(getApplicationContext(), BookActivity.class);
	         i.putExtra("ISBN", contents);
	         startActivity(i);
	      } else if (resultCode == RESULT_CANCELED) {
	         // Handle cancel
	      }
	   }
    }*/
   
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	//Get the result out of the ZXing Intent
	ZXingIntentResult scanResult = ZXingIntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
	if (scanResult != null && resultCode == RESULT_OK) {
		searchISBN(scanResult.getContents());
	} else if (resultCode == RESULT_CANCELED) {
		// do nothing
	}
	  // else continue with any other code you need in the method
    }
    
    private void searchISBN(String isbn) {
		SQLiteHelper sqlite_helper = new SQLiteHelper(getBaseContext());
		SQLiteDatabase sqldb = sqlite_helper.getWritableDatabase();
		
    	// lest check if book already exists in database
		Cursor cursor = sqldb.query("booksRead", new String[] { "bookName",
				"description", "author", "category", "ISBN", "flag", "_id" }, "ISBN LIKE '"+isbn+"%'", 
				null, null, null, null, null);

		boolean exists = (cursor.getCount() > 0);
		cursor.moveToFirst();
		
		if (exists) {
			
			Book book = new Book(cursor.getString(0), cursor.getString(1), cursor.getString(2), 
					cursor.getString(3), cursor.getString(4), cursor.getInt(5), cursor.getLong(6));
			
			Intent i = new Intent(getApplicationContext(), BookActivity.class);
	        i.putExtra("book", book);
	        startActivity(i);
		} else {
			String url = "https://www.googleapis.com/books/v1/volumes?q=isbn:"+isbn;
			Context context = getApplicationContext();
			
			NetworkTask networkTask = new NetworkTask(context, this);
			networkTask.execute(url, isbn);
			progressDialog = ProgressDialog.show(this, "", "Searching the book...");
		}
    }

	@Override
	public void onTaskComplete(Book result) {
		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		
		Intent i = new Intent(getApplicationContext(), BookActivity.class);
        i.putExtra("book", result);
        startActivity(i);
	}


}
